<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="项目名称" prop="name">
          <el-select v-model="form.name" placeholder="模糊搜索列表" clearable size="mini" style="width:100%">
            <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="文件类型" prop="name">
          <el-select v-model="form.busType" placeholder="请选择文件类型" clearable size="mini" style="width:100%">
            <el-option v-for="dict in dict.type.file_type" :key="dict.label" :label="dict.label" :value="dict.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="文件" prop="name">
          <FileUpload :value="upload.value" :limit="upload.limit" :fileSize="upload.fileSize" :fileType="upload.fileType" @uploadSuccess="uploadSuccess" />
          <!-- <el-upload>
            <el-button size="mini" type="primary">点击上传</el-button>
          </el-upload> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" size="mini">上 传</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postFilesAdd, getfiles, putfiles } from "@/api/business/project/information";
export default {
  dicts: ['file_type'],
  data() {
    return {
      title: '新增',
      open: false,
      form: {
        tabName: 'pm_projects',
        mainId: this.$route.query.mainId,
        originName: null,
        url: null,
        busType: null
      },
      upload: {
        value: [],
        url: '',
        limit: 1,
        fileSize: 10,
        fileType: ["doc", "xls", "ppt", 'xlsx', "txt", "pdf", 'png', 'jpg','jpeg'],
      },
      rules: {},
      mainId: null
    }
  },
  methods: {
    init(row) {
      this.open = true;
      this.upload.value = []
      this.form.busType = null
      this.resetForm("form");
      console.log(this.form.busType )
      if (!row) {
        this.title = '新增'
      } else {
        this.title = '修改'
        this.mainId = row.id
        this.getfiles()
      }

    },
    getfiles() {
      this.upload.value = []
      getfiles(this.mainId).then(respone => {
        this.form.busType = respone.data.busType;
        // this.upload.value = respone.data.originName
      })
    },
    uploadSuccess(file) {
      if (typeof file !== 'string') {
        this.form.originName = file.originName;
        this.form.url = file.fileName
      }
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.mainId) {
            postFilesAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$parent.getList()
            });
          } else {
            this.form['id'] = this.mainId
            putfiles(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$parent.getList()
            })
          }

        }
      });
    },
    cancel() {
      this.open = false;
    }
  }
}
</script>

<style lang="scss" scoped>
</style>